<?php	require_once(dirname(__FILE__).'/inc/config.inc.php');IsModelPriv('userfield_list');

/*
**************************
(C)2010-2014 phpMyWind.com
update: 2014-5-30 13:34:13
person: Feng
**************************
*/


//初始化参数
$tbname   = '#@__userfield';
$gourl    = 'userfield.php?cid='.$infotype;
$action   = isset($action)   ? $action   : '';
$infotype = isset($infotype) ? $infotype : '';
$fieldlong = isset($fieldlong) ? $fieldlong : '255';

//添加自定义字段
if($action == 'add')
{
	if(!isset($tbnames)){ShowMsg("参数错误！",'-1');exit();}

	$field = strtolower($fieldname);$fields = $dosql->get_fields($tbnames);
	if(array_key_exists($field, $fields)) 
	{
		ShowMsg("该字段名称已经存在！",'-1');
		exit();
	}
	
	//此类需要指定类型
	if($fieldtype == 'radio' or $fieldtype == 'checkbox' or $fieldtype == 'select' or $fieldtype == 'file')
	{
		$fieldtype2 = 'varchar';
	}
	else if($fieldtype == 'fileall')
	{
		$fieldtype2 = 'text';
	}
	else if($fieldtype == 'datetime')
	{
		$fieldtype2 = 'int';
		$fieldlong  = 10;
	}
	else if($fieldtype == 'number')
	{	
		if($fieldclass=='int')
			{	
				$fieldtype='int';
				$fieldtype2 = 'int';
			}
		else 
			{	
				$fieldtype = 'decimal';
				$fieldtype2 = 'decimal';
			}
		
	}
	
	if(empty($fieldtype2))
	{
		$fieldtype2 = $fieldtype;
	}
	if($fieldlong > 255)
	{
		ShowMsg("字段长度不能大于255",'-1');
		exit();
	}



	//开始组装SQL
	$sql = "ALTER TABLE `$tbnames` ADD `$fieldname` $fieldtype2";

	//此类型不需指定长度
	if($fieldtype2 != 'text' && $fieldtype2 != 'mediumtext')
	{	
		if(!isset($fieldlong))
		{
			ShowMsg("字段长度未设置",'-1');
			exit();
		}
		else{$sql .= "($fieldlong)";}
		
	}
	
	$sql .= " NOT NULL";


	//构成所属栏目
/* 	$catepriv = '';
	if(isset($classid) && is_array($classid))
	{
		$catepriv = implode(',',$classid);
	} */


	//为变量长度添加括号
	if($dosql->ExecNoneQuery($sql))
	{
		$sql = "INSERT INTO `$tbname` (infotype,fieldname, fieldtitle, fielddesc, fieldtype,fieldclass, fieldlong, fieldsel, fieldcheck, fieldcback, orderid,isreg,isadd, checkinfo) VALUES ('$infotype','$fieldname', '$fieldtitle', '$fielddesc', '$fieldtype','$fieldclass', '$fieldlong', '$fieldsel', '$fieldcheck', '$fieldcback', '$orderid', '$isreg','$isadd', '$checkinfo')";
		if($dosql->ExecNoneQuery($sql))
		{	
			header("location:$gourl");
			exit();
		}
	}
	else
	{
		ShowMsg("字段插入失败！请检查类型，长度是否合法！",'-1');
		exit();
	}
}


//修改自定义字段
else if($action == 'update')
{
	if($fieldname_b != $fieldname)
	{
		$field = strtolower($fieldname);$fields = $dosql->get_fields($tbnames);
		if(array_key_exists($field, $fields)) 
		{
			ShowMsg("该字段名称已经存在！",'-1');
			exit();
		}
	}


	//此类需要指定类型
if($fieldtype == 'radio' or $fieldtype == 'checkbox' or $fieldtype == 'select' or $fieldtype == 'file')
	{
		$fieldtype2 = 'varchar';
	}
	else if($fieldtype == 'fileall')
	{
		$fieldtype2 = 'text';
	}
	else if($fieldtype == 'datetime')
	{
		$fieldtype2 = 'int';
		$fieldlong  = 10;
	}
	else if($fieldtype == 'number')
	{	
		if($fieldclass=='int')
			{	
				$fieldtype='int';
				$fieldtype2 = 'int';
			}
		else 
			{	
				$fieldtype = 'decimal';
				$fieldtype2 = 'decimal';
			}
		
	}
	
	if(empty($fieldtype2))
	{
		$fieldtype2 = $fieldtype;
	}


	//开始组装SQL
	$sql = "ALTER TABLE `$tbnames` CHANGE `$fieldoldname` `$fieldname` $fieldtype2";

	if($fieldtype2 != 'text' && $fieldtype2 != 'mediumtext')
	{
		$sql .= "($fieldlong)";
	}

	if($fieldtype2 == 'varchar' || $fieldtype2 == 'text' || $fieldtype2 == 'mediumtext')
	{
		$sql .= " CHARACTER SET utf8 COLLATE utf8_general_ci";
	}

	$sql .= " NOT NULL";


	if($fieldlong > 255)
	{
		ShowMsg("字段长度不能大于255",'-1');
		exit();
	}

	if($dosql->ExecNoneQuery($sql))
	{
		$sql = "UPDATE `$tbname` SET  fieldname='$fieldname', fieldtitle='$fieldtitle', fielddesc='$fielddesc', fieldtype='$fieldtype',fieldclass='$fieldclass', fieldlong='$fieldlong', fieldsel='$fieldsel', fieldcheck='$fieldcheck', fieldcback='$fieldcback',isreg='$isreg',isadd='$isadd', orderid='$orderid', checkinfo='$checkinfo' WHERE id=$id";

		if($dosql->ExecNoneQuery($sql))
		{
			header("location:$gourl");
			exit();
		}
	}
	else
	{
		ShowMsg("字段插入失败！请检查类型，长度是否合法！",'-1');
		exit();
	}
}


//自定义字段状态
else if($action == 'check')
{
	if($checkinfo == 'true')
	{
		$sql = "UPDATE `$tbname` SET checkinfo='false' WHERE id=$id";
	}
	else
	{
		$sql = "UPDATE `$tbname` SET checkinfo='true' WHERE id=$id";
	}

	if($dosql->ExecNoneQuery($sql))
	{
		header("location:$gourl");
		exit();
	}
}


//删除单条自定义字段
else if($action == 'del')
{
	$dosql->ExecNoneQuery("ALTER TABLE `$tbnames` DROP `$fieldname`");
	$dosql->ExecNoneQuery("DELETE FROM `$tbname` WHERE `id`=$id");
	header("location:$gourl");
	exit();
}


//无条件返回
else
{
    header("location:$gourl");
	exit();
}
?>